<template>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="700px" append-to-body>
        <el-form ref="form" :model="model" :rules="rules" inline label-width="170px" label-suffix=":" class="form-class" v-loading="loading">
            <el-form-item label="美金汇率" prop="dollar">
                <el-input v-model.trim="model.dollar" placeholder="请输入美金汇率" clearable></el-input>
            </el-form-item>
            <el-form-item label="欧元汇率" prop="euro_exchange_rate">
                <el-input v-model.trim="model.euro_exchange_rate" placeholder="请输入欧元汇率" clearable></el-input>
            </el-form-item>
            <el-form-item label="英镑汇率" prop="gbp_exchange_rate">
                <el-input v-model.trim="model.gbp_exchange_rate" placeholder="请输入英镑汇率" clearable></el-input>
            </el-form-item>
            <el-form-item label="预估运费单价¥" prop="single_price">
                <el-input v-model.trim="model.single_price" placeholder="请输入打包费-¥" clearable></el-input>
            </el-form-item>
            <el-form-item label="挂号费¥" prop="register_fee">
                <el-input v-model.trim="model.register_fee" placeholder="请输入打包费-¥" clearable></el-input>
            </el-form-item>
            <el-form-item label="打包费$" prop="LogisticsMarkupFee">
                <el-input v-model.trim="model.LogisticsMarkupFee" placeholder="请输入打包费-$" clearable></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" :loading="save" :disabled="save" @click="submit">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
import { queryOrderExchange, editOrderExchange } from '@/api/order'

export default {
    name: 'DetailDialog',
    props: {
        value: {
            type: Boolean,
            default: false,
            required: true,
        },
        params: {
            type: Object,
            default: () => {},
        },
    },
    data() {
        return {
            loading: false,
            save: false,
            model: {
                id: '',
                dollar: '',
                euro_exchange_rate: '',
                gbp_exchange_rate: '',
                mexicanpeso: '',
                LogisticsMarkupFee: ''
            },
            rules: {
                dollar: [{ required: true, message: '美金汇率不能为空', trigger: ['blur', 'change'] }],
                mexicanpeso: [{ required: true, message: '墨西哥比索汇率不能为空', trigger: ['blur', 'change'] }],
                LogisticsMarkupFee: [{ required: true, message: '打包费$不能为空', trigger: ['blur', 'change'] }],
            },
        }
    },
    computed: {
        title() {
            return `${this.model.id ? '编辑' : '新增'}每月预估值`
        },
        dialogVisible: {
            get() {
                return this.value
            },
            set(val) {
                this.$emit('input', val)
            },
        },
    },
    watch: {
        params: {
            handler(val) {
                if (val.id) {
                    this.getDetail()
                }
            },
            immediate: true,
            deep: true,
        },
    },
    mounted() {
        // this.getDetail()
    },
    methods: {
        getDetail() {
            this.loading = true
            const params = { id: this.params.id }
            queryOrderExchange(params)
                .then(res => {
                    this.model = Object.assign({}, this.model, res.data)
                })
                .finally(() => {
                    this.loading = false
                })
        },
        submit() {
            this.$refs.form.validate(valid => {
                if (!valid) {
                    return
                }

                const params = { ...this.model }
                if (this.save) {
                    return
                }
                this.save = true
                editOrderExchange(params)
                    .then(res => {
                        this.$message.success(res.msg)
                        this.dialogVisible = false
                        this.$emit('success')
                    })
                    .finally(() => {
                        this.save = false
                    })
            })
        },
        cancel() {
            this.dialogVisible = false
        },
    },
}
</script>

<style>
.form-class.el-form .el-form-item {
    width: 100%;
    margin-right: 0;
}
.form-class.el-form .el-form-item .el-input {
    width: 400px;
}
</style>
